import { Diagnostic } from './languageFeatures';
import { worker, editor } from 'monaco-editor';

//TODO
export type StructMap = {};

/**
 * Immutable , init once
 */
export interface IWorkerImmutable {
	structMap: StructMap;
}

export abstract class BaseWorker {
	protected _ctx: worker.IWorkerContext;
	protected structMap: StructMap;
	public constructor(ctx: worker.IWorkerContext, createData: IWorkerImmutable) {
		this._ctx = ctx;
		this.structMap = createData.structMap;
	}

	async doValidation(uri: string): Promise<editor.IMarkerData[]> {
		const syntaxError = await this.getSyntacticDiagnostics(uri);
		if (syntaxError.length) return syntaxError;
		return this.getSemanticDiagnostics(uri);
	}

	/**
	 * Get diagnostic messages for any syntax issues in the given file.
	 */
	abstract getSyntacticDiagnostics(uri: string): Promise<Diagnostic[]>;

	/**
	 * Get diagnostic messages for any semantic issues in the given file.
	 */
	abstract getSemanticDiagnostics(uri: string): Promise<Diagnostic[]>;

	private getModel(fileName: string): worker.IMirrorModel | null {
		let models = this._ctx.getMirrorModels();
		for (let i = 0; i < models.length; i++) {
			if (models[i].uri.toString() === fileName) {
				return models[i];
			}
		}
		return null;
	}
	getScriptText(fileName: string): string | undefined {
		let text: string;
		let model = this.getModel(fileName);
		if (model) {
			// a true editor model
			text = model.getValue();
		} else {
			return;
		}

		return text;
	}
}
